<?php
/*
  $Id: pos_payment.php,v 1.1.1.1 2004/03/04 23:40:46 ccwjr Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

  class pos_payment {
    var $modules, $selected_module;

// class constructor
    function pos_payment($module = '') {
// BOF: WebMakers.com Added: Downloads Controller
      global $pos_payment, $language, $PHP_SELF, $cart;
// EOF: WebMakers.com Added: Downloads Controller

      if (defined('MODULE_PAYMENT_INSTALLED') && tep_not_null(MODULE_PAYMENT_INSTALLED)) {
      //Eversun mod for sppc && qty price breaks
//       $this->modules = explode(';', MODULE_PAYMENT_INSTALLED);
     global $sppc_customer_group_id, $customer_id;
     if(!tep_session_is_registered('sppc_customer_group_id')) {
      $customer_group_id = '0';
     } else {
      $customer_group_id = $sppc_customer_group_id;
     }
     $customer_pos_payment_query = tep_db_query("select IF(c.customers_payment_allowed <> '', c.customers_payment_allowed, cg.group_payment_allowed) as pos_payment_allowed from " . TABLE_CUSTOMERS . " c, " . TABLE_CUSTOMERS_GROUPS . " cg where c.customers_id = '" . $customer_id . "' && cg.customers_group_id =  '" . $customer_group_id . "'");
     if ($customer_pos_payment = tep_db_fetch_array($customer_pos_payment_query)  ) {
       if (tep_not_null($customer_pos_payment['pos_payment_allowed'])) {
        $temp_pos_payment_array = explode(';', $customer_pos_payment['pos_payment_allowed']);
        $installed_modules = explode(';', MODULE_PAYMENT_INSTALLED);
        for ($n = 0; $n < sizeof($installed_modules) ; $n++) {
          // check to see if a pos_payment method is not de-installed
          if ( in_array($installed_modules[$n], $temp_pos_payment_array ) ) {
            $pos_payment_array[] = $installed_modules[$n];
          }
        } // end for loop
        if (is_array($pos_payment_array)) {
                                $this->modules = $pos_payment_array;
                          } else {
                                $this->modules = array();
                          }
        } else {
        $this->modules = explode(';', MODULE_PAYMENT_INSTALLED);
        }
      } else { // default
        $this->modules = explode(';', MODULE_PAYMENT_INSTALLED);
      }
// Eversun mod end for sppc && qty price breaks


        $include_modules = array();

        if ( (tep_not_null($module)) && (in_array($module . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1)), $this->modules)) ) {
          $this->selected_module = $module;

          $include_modules[] = array('class' => $module, 'file' => $module . '.php');
        } else {
          reset($this->modules);
// BOF: WebMakers.com Added: Downloads Controller - Free Shipping && Payments
// Show either normal pos_payment modules or free pos_payment module when Free Shipping Module is On
          // Free Payment Only

          if (tep_get_configuration_key_value('MODULE_PAYMENT_FREECHARGER_STATUS') && ((isset($cart->show_total)) && $cart->show_total()==0) && ((isset($cart->show_weight)) && $cart->show_weight==0 ) ) {
            $this->selected_module = $module;
            $include_modules[] = array('class'=> 'freecharger', 'file' => 'freecharger.php');
          } else {
            // All Other Payment Modules
            while (list(, $value) = each($this->modules)) {
              $class = substr($value, 0, strrpos($value, '.'));
              // Don't show Free Payment Module
              if ($class !='freecharger') {
                $include_modules[] = array('class' => $class, 'file' => $value);
              }
            }
// EOF: WebMakers.com Added: Downloads Controller
          }
        }

        for ($i=0, $n=sizeof($include_modules); $i<$n; $i++) {
          include(DIR_WS_LANGUAGES . $language . '/pos_modules/payment/' . $include_modules[$i]['file']);
          include(DIR_FS_ADMIN_MODULES . 'payment/' . $include_modules[$i]['file']);

          $GLOBALS[$include_modules[$i]['class']] = new $include_modules[$i]['class'];
        }

// if there is only one pos_payment method, select it as default because in
// checkout_confirmation.php the $pos_payment variable is being assigned the
// $_POST['pos_payment'] value which will be empty (no radio button selection possible)
        if ( (tep_count_pos_payment_modules() == 1) && (!isset($GLOBALS[$pos_payment]) || (isset($GLOBALS[$pos_payment]) && !is_object($GLOBALS[$pos_payment]))) ) {
          $pos_payment = $include_modules[0]['class'];
          $this->selected_module = $pos_payment;
        }

        if ( (tep_not_null($module)) && (in_array($module, $this->modules)) && (isset($GLOBALS[$module]->form_action_url)) ) {
          $this->form_action_url = $GLOBALS[$module]->form_action_url;
        }
      }
    }

// class methods
/* The following method is needed in the checkout_confirmation.php page
   due to a chicken && egg problem with the pos_payment class && order class.
   The pos_payment modules needs the order destination data for the dynamic status
   feature, && the order class needs the pos_payment module title.
   The following method is a work-around to implementing the method in all
   pos_payment modules available which would break the modules in the contributions
   section. This should be looked into again post 2.2.
*/
    function update_status() {
      if (is_array($this->modules)) {
        if (is_object($GLOBALS[$this->selected_module])) {
          if (function_exists('method_exists')) {
            if (method_exists($GLOBALS[$this->selected_module], 'update_status')) {
              $GLOBALS[$this->selected_module]->update_status();
            }
          } else { // PHP3 compatibility
            @call_user_method('update_status', $GLOBALS[$this->selected_module]);
          }
        }
      }
    }

    function javascript_validation() {
      $js = '';
      if (is_array($this->modules)) {
        $js = '<script type="text/javascript"><!-- ' . "\n" .
              'function check_form() {' . "\n" .
              '  var error = 0;' . "\n" .
              '  var error_message = "' . JS_ERROR . '";' . "\n" .
              '  var pos_payment_value = null;' . "\n" .
              '  if (document.checkout_pos_payment.pos_payment.length) {' . "\n" .
              '    for (var i=0; i<document.checkout_pos_payment.pos_payment.length; i++) {' . "\n" .
              '      if (document.checkout_pos_payment.pos_payment[i].checked) {' . "\n" .
              '        pos_payment_value = document.checkout_pos_payment.pos_payment[i].value;' . "\n" .
              '      }' . "\n" .
              '    }' . "\n" .
              '  } else if (document.checkout_pos_payment.pos_payment.checked) {' . "\n" .
              '    pos_payment_value = document.checkout_pos_payment.pos_payment.value;' . "\n" .
              '  } else if (document.checkout_pos_payment.pos_payment.value) {' . "\n" .
              '    pos_payment_value = document.checkout_pos_payment.pos_payment.value;' . "\n" .
              '  }' . "\n\n";

        reset($this->modules);
        while (list(, $value) = each($this->modules)) {
          $class = substr($value, 0, strrpos($value, '.'));
          if ($GLOBALS[$class]->enabled) {
            $js .= $GLOBALS[$class]->javascript_validation();
          }
        }

        $js .= "\n" . '  if (pos_payment_value == null && submitter != 1) {' . "\n" . // ICW CREDIT CLASS Gift Voucher System
               '    error_message = error_message + "' . JS_ERROR_NO_PAYMENT_MODULE_SELECTED . '";' . "\n" .
               '    error = 1;' . "\n" .
               '  }' . "\n\n" .
//  ICW CREDIT CLASS Gift Voucher System Line below amended
               '  if (error == 1 && submitter != 1) {' . "\n" .
               '    alert(error_message);' . "\n" .
               '    return false;' . "\n" .
               '  } else {' . "\n" .
               '    return true;' . "\n" .
               '  }' . "\n" .
               '}' . "\n" .
               '//--></script>' . "\n";
      }

      return $js;
    }

    function selection() {
      $selection_array = array();

      if (is_array($this->modules)) {
        reset($this->modules);
        while (list(, $value) = each($this->modules)) {
          $class = substr($value, 0, strrpos($value, '.'));
          if ($GLOBALS[$class]->enabled) {
            $selection = $GLOBALS[$class]->selection();
            if (is_array($selection) && sizeof($selection) > 0) $selection_array[] = $selection;
          }
        }
      }

      return $selection_array;
    }
 //ICW CREDIT CLASS Gift Voucher System
 // check credit covers was setup to test whether credit covers is set in other parts of the code
function check_credit_covers() {
  global $credit_covers;

  return $credit_covers;
}
    function pre_confirmation_check() {
      global $credit_covers, $pos_payment_modules; //ICW CREDIT CLASS Gift Voucher System
      if (is_array($this->modules)) {
        if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {

          if ($credit_covers) { //  ICW CREDIT CLASS Gift Voucher System
            $GLOBALS[$this->selected_module]->enabled = false; //ICW CREDIT CLASS Gift Voucher System
            $GLOBALS[$this->selected_module] = NULL; //ICW CREDIT CLASS Gift Voucher System
            $pos_payment_modules = ''; //ICW CREDIT CLASS Gift Voucher System
          } else { //ICW CREDIT CLASS Gift Voucher System
            $GLOBALS[$this->selected_module]->pre_confirmation_check();
          }
        }
      }
    } //ICW CREDIT CLASS Gift Voucher System

    function confirmation() {
      if (is_array($this->modules)) {
        if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {
          return $GLOBALS[$this->selected_module]->confirmation();
        }
      }
    }

    function process_button() {
      if (is_array($this->modules)) {
        if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {
          return $GLOBALS[$this->selected_module]->process_button();
        }
      }
    }

    function before_process() {
      if (is_array($this->modules)) {
        if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {
          return $GLOBALS[$this->selected_module]->before_process();
        }
      }
    }

    function after_process() {
      if (is_array($this->modules)) {
        if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {
          return $GLOBALS[$this->selected_module]->after_process();
        }
      }
    }

    function get_error() {
      if (is_array($this->modules)) {
        if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {
          return $GLOBALS[$this->selected_module]->get_error();
        }
      }
    }
  }
?>
